#include "wm.h"

static void set_over(SDL_Event event, s_object *object)
{
    int pos[2] = {0, 0};

    pos[1] = event.button.x / IMG_X;
    pos[0] = event.button.y / IMG_Y;
    if (pos[0] < MAT_H && pos[1] < MAT_W &&
        object->mat[pos[0]][pos[1]] == B_GRASS)
    {
        add_to_tow_list(object->tow_type, object, pos);
    }
}

void mouse_click(s_object *object, SDL_Event event)
{
    switch (event.type)
    {
        case SDL_MOUSEBUTTONDOWN:
            if (event.button.button == SDL_BUTTON_LEFT)
                object->bpushd = 1;
            else if (event.button.button == SDL_BUTTON_RIGHT)
                object->bpushd = 1;
            break;
        case SDL_MOUSEBUTTONUP:
            if (object->bpushd && event.button.button == SDL_BUTTON_LEFT)
            {
                object->bpushd = 0;
                set_over(event, object);
            }
            else if (object->bpushd && event.button.button == SDL_BUTTON_RIGHT)
            {
                object->bpushd = 0;
                object->tow_type = (object->tow_type + 1) % TOW_SIZE;
            }
            break;
        case SDL_MOUSEMOTION:
            break;
        default:
            break;
    }
}

